
angular.module('panels')
    .directive('panelItilToDoTaskGx', function ($http, tabService, LoginInfo, loadConf, $timeout, $log, Promise) {
        return {
            link: function (scope) {
                Promise.all({
                    setting: loadConf('panel-itil-todo-task-gx.yml'),
                    loginInfo: LoginInfo
                }).then(function (result) {
                    var setting = result.setting, loginInfo = result.loginInfo, timeoutId;
                    scope.more = setting.more;
                    if (setting.processList) {
                        scope.processList = setting.processList;
                    }
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        $http.jsonp(setting.countUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID).replace('{T}',new Date().getTime())).success(function (data) {
                            if (scope.processList) {
                                scope.todoList=[];
                                var i=0;
                                angular.forEach(scope.processList, function (todo) {
                                    if (data[0][todo.id]>0){
                                        var todoMap = {};
                                        todoMap = todo;
                                        todoMap['count'] = data[0][todo.id];
                                        scope.todoList[i++] = todoMap;
                                    }
                                });
                            }
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }
                        $log.debug('panel-update data: itil-to-do-task-gx');
                    }());
                    scope.openTodoListPage = function (todo) {
                        tabService.addTab({
                            name: todo.name,
                            url: todo.viewUrl
                        });
                    };

                });
            },
            templateUrl: 'app/panel/itil/to-do-task-gx.html'
        };
    });